<?php
class Journal_Block_Article extends Epic_View_Block {	
	public function getAllJournal ($ft,$topic=null){
		if ($ft!=null)
			return Epic::getModel('journal/Journal')->getCollection()
				->where('name',array ('like'=>"'".$ft."%'"))
				->load();
		if ($topic!=null) {
			return Epic::getModel('journal/Pager')->getCollection()
				-> where ('id_pager', array ('in'=>'(SELECT id_pager FROM topic_pager WHERE id_topic='.$topic.')'))
				-> groupby (array (0=>'id_journal'))
				-> load();
		}
		return Epic::getModel('journal/Journal')->getCollection()
			->load();
			
	}
	public function getAllPagerOfJournal ($id_journal) {
		if (isset($_POST['volume']) || isset($_POST['doi'])) {
			$volume = $_POST['volume'];
			$doi = $_POST['doi'];
			$issue = $_POST['issue'];
			$page_start = $_POST['page_start'];
			$where = 'id_journal='.$id_journal;
			if ($volume !="") 	$where.=" AND volume=".$volume;
			if ($doi !="") 	$where.=" AND doi = ".$doi;
			if ($issue !="") 	$where.=" AND issue =".$issue;
			if ($page_start !="") 	$where.=" AND page_start =".$page_start;
			return Epic::getAdapter()->query('SELECT * 
		        						FROM `pager` 
		        							where '.$where)
  				  			 ->fetchAll();
		}
		return Epic::getModel('journal/Pager')->getCollection()
			->where('id_journal', array('eq' => $id_journal))
			->load();					
	}
	public function getAllYearOfJournal  ($id_journal) {
		return Epic::getModel('journal/Pager')->getCollection()
			->field('year')
			->where('id_journal', array('eq' => $id_journal))
			->groupBy (array ('year'))
			->load();
	}
	public function getTopics () {
		return Epic::getModel('journal/Topic')->getCollection()
			->load();
	}
	public function getPager($id_pager){
		return Epic::getModel('journal/Pager')
			->load($id_pager);
	}
	
	public function getkeywords($id_pager){
		return Epic::getModel('journal/Pager')->getCollection()
			->field('id_pager')
			->join ('keyword_pager as kp','(kp.id_pager=m.id_pager)',array())
			->join ('keyword as k','(k.id_keyword=kp.id_keyword)',array('name_keyword'=>'k.name_keyword'))
			->where('m.id_pager', array('eq' => $id_pager))
			->load();
	}
	// get array first letter in set titles of journal
	public function getFirstLetters () {
		$journals = Epic::getModel('journal/Journal')->getCollection()
					->field('name')
					->load();
		$rs = array ();
		for ($i=0;$i<26;$i++){
			$rs[strtoupper(chr(65+$i))] = 0;
		}
		foreach ($journals as $journal) {
			$fl = substr(strtoupper($journal['name']), 0,1);
			$rs[$fl] = 1;
		}
		return $rs;
	}
	public function getKeyword($id_keyword){
		return Epic::getModel('journal/Keyword')->getCollection()
					->where('id_keyword', array('eq' => $id_keyword))
					->load();
	}
	public function getNews () {
		return Epic::getModel('journal/journal')->getCollection()
			->load();
	}
	public function getPopulation () {
		return Epic::getModel('journal/journal')->getCollection()
			->load();
	}
}